<?php

namespace app\api\controller;

use app\common\controller\Api;
use app\admin\model\jubao\Hsh;
use app\admin\model\jubao\Wcn;
use app\admin\model\jubao\Yl;
use think\Validate;

/**
 * 示例接口
 */
class Report extends Api
{

    //如果$noNeedLogin为空表示所有接口都需要登录才能请求
    //如果$noNeedRight为空表示所有接口都需要验证权限才能请求
    //如果接口已经设置无需登录,那也就无需鉴权了
    //
    // 无需登录的接口,*表示全部
    protected $noNeedLogin = ['*'];
    // 无需鉴权的接口,*表示全部
    protected $noNeedRight = ['*'];

    /**
     * 养老保险诈骗
     * @author 274589954@qq.com
     * @since  2022-07-01
     * @return [type]     [description]
     */
    public function endowmentInsuranceFraud()
    {
        $data = $this->request->param();
        $token = $this->request->header('token','');
        $rule = [
            'type'  => 'require|in:personal,company',
            // 'company'   => 'requireIf:type,company',
            // 'name' => 'require',
            // 'phone' => 'require',
            // 'id_card'   => 'requireIf:type,personal',
            // 'address' => 'require',
            'r_type'  => 'require|in:personal,company',
            'r_name'  => 'require',
            'r_sex'   => 'requireIf:r_type,personal|in:0,1,2',
            'r_job'   => 'requireIf:r_type,personal',
            'r_phone'   => 'require',
            'r_address'=>'require',
            'r_birthplace'=>'requireIf:r_type,personal',
            'f_address'=>'require',
            'f_starttime'=>'require',
            'f_endtime'=>'require|egt:f_starttime',
            'f_isgang'=>'require|in:0,1,2',
            'f_money'=>'require',
            'f_num'=>'require|in:0,1,2,4,11,50',
            'f_type'=>'require|array',
            // 'f_type'=>'require|in:1,2,3,4,5,6,7,8,9,10,11,12,13',
            // 'f_charge'=>'require|in:1,2,3,4,5,6,7,8,9,10,11,12',
            'f_charge'=>'require|array',
            'f_content'=>'require',
            'captcha'=>'require|captcha:api'.$token,
        ];

        $msg = [
            'type.require' => '请选择个人名义、单位名义举报',
            // 'type.require' => '{%name_require}',
            'type.in'     => '请选择个人名义、单位名义举报',
            'company.requireIf'   => '请填写单位名称举报',
            'name.require'  => '请填写联系人姓名',
            'phone.require'        => '请填写联系人电话',
            'id_card.requireIf'        => '请填写举报人身份证号码',
            'address.require'        => '请填写举报联系地址',
            'r_type.require' => '请选择被举报个人、单位',
            'r_name.require' => '请填写被举报人(被举报单位)名称',
            'r_sex.requireIf'   => '请选择被举报人性别',
            'r_sex.in'   => '请选择被举报人性别',
            'r_job.requireIf'   => '请填写被举报人职务',
            'r_phone.require'   => '请填写被举报人(被举报单位)的联系电话',
            'r_id_info.requireIf'   => '请选择被举报人身份',
            'r_id_info.in'   => '请选择被举报人身份',
            'r_address.require'   => '请填写被举报人(被举报单位)现住详址',
            'r_birthplace.requireIf'   => '请填写被举报人户籍地详址',
            'f_address.require'   => '请填写违法犯罪主要发生地',
            'f_starttime.require'   => '请填写违法犯罪开始时间',
            'f_endtime.require'   => '请填写违法犯罪结束时间',
            'f_endtime.egt'   => '违法犯罪结束时间要大于开始时间',
            'f_isgang.require'   => '请选择是否团伙犯罪',
            'f_isgang.in'   => '请选择是否团伙犯罪',
            'f_money.require'   => '请填写涉案金额（单位:元）',
            'f_num.require'   => '请选择受害人数',
            'f_num.in'   => '请选择受害人数',
            'f_type.require'   => '请选择犯罪的问题类别',
            // 'f_type.in'   => '请选择犯罪的问题类别',
            'f_type.array'   => '请选择犯罪的问题类别',
            'f_charge.require'   => '请选择涉嫌罪名',
            'f_charge.array'   => '请选择涉嫌罪名',
            // 'f_charge.in'   => '请选择涉嫌罪名',
            'f_content.require'   => '请填写犯罪事实',
            'captcha.require'   => '请填写验证码',
            
        ];
        
        $result = $this->validate($data,$rule,$msg);
        if(true !== $result){
            // 验证失败 输出错误信息
            $this->error($result);
        }
        $this->checkImagesAndVideos($data);
        // var_dump($data);exit;
        $data['no'] = $this->createNo();

        $ylMod = (new Yl);
        $ftypes = $ylMod->getFTypeList();
        $data['f_type'] = array_intersect(array_keys($ftypes), $data['f_type']);
        if (!$data['f_type']) {
            $this->error('请选择犯罪的问题类别');
        }
        $data['f_type'] = implode(',', $data['f_type']);

        $fCharge = $ylMod->getFChargeList();
        $data['f_charge'] = array_intersect(array_keys($fCharge), $data['f_charge']);
        if (!$data['f_charge']) {
            $this->error('请选择涉嫌罪名');
        }
        $data['f_charge'] = implode(',', $data['f_charge']);
        $data['f_starttime'] = strtotime($data['f_starttime'].'0101');
        $data['f_endtime'] = strtotime($data['f_endtime'].'0101');
        // isset($data['id'])
        // unset($data['id']);
        $id = (new Yl)->endowmentInsuranceFraud($data);
        if ($id) {
            $this->success('成功', ['no'=>$data['no']]);
        }else{
            $this->error('失败');
        }
    }
    /**
     * 检查图片、视频
     * @author 274589954@qq.com
     * @since  2022-07-05
     * @param  [type]     &$data [description]
     * @return [type]            [description]
     */
    protected function checkImagesAndVideos(&$data)
    {
        if (!empty($data['images']) && is_array($data['images'])) {
            $images = [];
            foreach ($data['images'] as $value) {
                // var_dump(parse_url($value));
                // if(!Validate::is($value,'url')){
                if(!is_string($value) ){
                    $this->error('请上传图片'.$value);
                }
                $images[] = $value;
            }
            $data['images'] = implode(',', $images);
        }else{
            $data['images'] = '';
        }
        if (!empty($data['video_images']) && is_array($data['video_images'])) {
            $videos = [];
            foreach ($data['video_images'] as $value) {
                if(!is_string($value)){
                    $this->error('请上传图片');
                }
                $videos[] = $value;
            }
            $data['video_images'] = implode(',',$videos);
        }else{
            $data['video_images'] = '';
        }
    }
    /**
     * 侵害未成年人
     * @author 274589954@qq.com
     * @since  2022-07-01
     * @return [type]     [description]
     */
    public function harmMinors()
    {
        $data = $this->request->param();
        $token = $this->request->header('token','');
        $rule = [
            'type'  => 'require|in:personal,company',
            // 'company'   => 'requireIf:type,company',
            // 'name' => 'require',
            // 'phone' => 'require',
            // 'id_card'   => 'requireIf:type,personal',
            // 'address' => 'require',
            'r_type'  => 'require|in:personal,company',
            'r_name'  => 'require',
            'r_sex'   => 'requireIf:r_type,personal|in:0,1,2',
            'r_job'   => 'requireIf:r_type,personal',
            'r_phone'   => 'require',
            'r_address'=>'require',
            'r_birthplace'=>'requireIf:r_type,personal',
            'f_address'=>'require',
            'f_starttime'=>'require',
            'f_endtime'=>'require|egt:f_starttime',
            'f_isgang'=>'require|in:0,1,2',
            'f_num'=>'require|in:0,1,2,4,11,50',
            'f_type'=>'require|array',
            // 'f_type'=>'require|array|in:1,2,3,4,5,6,7',
            'f_content'=>'require',
            'captcha'=>'require|captcha:api'.$token,
        ];
        $msg = [
            'type.require' => '请选择个人名义、单位名义举报',
            // 'type.require' => '{%name_require}',
            'type.in'     => '请选择个人名义、单位名义举报',
            'company.requireIf'   => '请填写单位名称举报',
            'name.require'  => '请填写联系人姓名',
            'phone.require'        => '请填写联系人电话',
            'id_card.requireIf'        => '请填写举报人身份证号码',
            'address.require'        => '请填写举报联系地址',
            'r_type.require' => '请选择被举报个人、单位',
            'r_name.require' => '请填写被举报人(被举报单位)名称',
            'r_sex.requireIf'   => '请选择被举报人性别',
            'r_sex.in'   => '请选择被举报人性别',
            'r_job.requireIf'   => '请填写被举报人职务',
            'r_phone.require'   => '请填写被举报人(被举报单位)的联系电话',
            'r_id_info.requireIf'   => '请选择被举报人身份',
            'r_id_info.in'   => '请选择被举报人身份',
            'r_address.require'   => '请填写被举报人(被举报单位)现住详址',
            'r_birthplace.requireIf'   => '请填写被举报人户籍地详址',
            'f_address.require'   => '请填写违法犯罪主要发生地',
            'f_starttime.require'   => '请填写违法犯罪开始时间',
            'f_endtime.require'   => '请填写违法犯罪结束时间',
            'f_endtime.egt'   => '违法犯罪结束时间要大于开始时间',
            'f_isgang.require'   => '请选择是否团伙犯罪',
            'f_isgang.in'   => '请选择是否团伙犯罪',
            'f_num.require'   => '请选择受害人数',
            'f_num.in'   => '请选择受害人数',
            'f_type.require'   => '请选择犯罪的问题类别',
            'f_type.array'   => '犯罪的问题类别为多选',
            // 'f_type.in'   => '请选择犯罪的问题类别1',
            'f_content.require'   => '请填写犯罪事实',
            'captcha.require'   => '请填写验证码',
        ];
        
        $result = $this->validate($data,$rule,$msg);
        if(true !== $result){
            // 验证失败 输出错误信息
            $this->error($result);
        }
        $wcnMod = (new Wcn);
        
        $ftypes = $wcnMod->getFTypeList();
        $data['f_type'] = array_intersect(array_keys($ftypes), $data['f_type']);
        if (!$data['f_type']) {
            $this->error('请选择犯罪的问题类别');
        }
        $data['f_type'] = implode(',', $data['f_type']);
        $data['f_starttime'] = strtotime($data['f_starttime'].'0101');
        $data['f_endtime'] = strtotime($data['f_endtime'].'0101');

        $this->checkImagesAndVideos($data);

        $data['no'] = $this->createNo();
        
        // isset($data['id'])
        // unset($data['id']);
        $id = (new Wcn)->harmMinors($data);
        if ($id) {
            $this->success('成功', ['no'=>$data['no']]);
        }else{
            $this->error('失败');
        }
    }
    /**
     * 扫黑举报
     * @author 274589954@qq.com
     * @since  2022-07-01
     * @return [type]     [description]
     */
    public function gangCrimes()
    {
        $data = $this->request->param();
        $token = $this->request->header('token','');
        $rule = [
            'type'  => 'require|in:personal,company',
            // 'company'   => 'requireIf:type,company',
            // 'name' => 'require',
            // 'phone' => 'require',
            // 'address' => 'require',
            'r_type'  => 'require|in:personal,company',
            'r_name'  => 'require',
            'r_sex'   => 'requireIf:r_type,personal|in:0,1,2',
            'r_job'   => 'requireIf:r_type,personal',
            'r_id_info'   => 'requireIf:r_type,personal|in:0,1,2,3,4,5,6,7',
            'r_address'=>'require',
            'f_address'=>'require',
            'f_starttime'=>'require',
            'f_endtime'=>'require|egt:f_starttime',
            'f_gang'=>'require|in:1,2,3,4,5',
            'f_charge'=>'require|array',
            // 'f_charge'=>'require|in:1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19',
            'f_industry'=>'require|array',
            // 'f_industry'=>'require|in:1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16',
            'f_content'=>'require',
            's_exist'=>'require|in:0,1,2',
            // 's_name'=>'requireIf:s_exist,1',
            // 's_id_info'=>'requireIf:s_exist,1|in:1,2,3,4,5,6,0',
            // 's_job'=>'requireIf:s_exist,1',
            // 's_content'=>'requireIf:s_exist,1',
            'captcha'=>'require|captcha:api'.$token,
        ];
        $msg = [
            'type.require' => '请选择个人名义、单位名义举报',
            // 'type.require' => '{%name_require}',
            'type.in'     => '请选择个人名义、单位名义举报',
            'company.requireIf'   => '请填写单位名称举报',
            'name.require'  => '请填写联系人姓名',
            'phone.require'        => '请填写联系人电话',
            'address.require'        => '请填写举报联系地址',
            'r_type.require' => '请选择被举报个人、单位',
            'r_name.require' => '请填写被举报人(被举报单位)名称',
            'r_sex.requireIf'   => '请选择被举报人性别',
            'r_sex.in'   => '请选择被举报人性别',
            'r_job.requireIf'   => '请填写被举报人职务',
            'r_id_info.requireIf'   => '请选择被举报人身份',
            'r_id_info.in'   => '请选择被举报人身份',
            'r_address.require'   => '请填写被举报人(单位)现住详址',
            'f_address.require'   => '请填写违法犯罪主要发生地',
            'f_starttime.require'   => '请填写违法犯罪开始时间',
            'f_endtime.require'   => '请填写违法犯罪结束时间',
            'f_endtime.egt'   => '违法犯罪结束时间要大于开始时间',
            'f_gang.require'   => '请选择犯罪团伙规模',
            'f_gang.in'   => '请选择犯罪团伙规模',
            'f_charge.require'   => '请选择涉嫌罪名',
            'f_charge.array'   => '请选择涉嫌罪名',
            // 'f_charge.in'   => '请选择涉嫌罪名',
            'f_industry.require'   => '请选择涉及行业',
            'f_industry.array'   => '请选择涉及行业',
            // 'f_industry.in'   => '请选择涉及行业',
            'f_content.require'   => '请填写犯罪事实',
            's_exist.require'   => '请选择是否有保护伞',
            's_exist.in'   => '请选择是否有保护伞',
            // 's_name.requireIf'   => '请填写保护伞姓名',
            // 's_id_info.requireIf'   => '请选择保护伞身份信息',
            // 's_id_info.in'   => '请选择保护伞身份信息',
            // 's_job.requireIf'   => '请填写保护伞职务',
            // 's_content.requireIf'   => '请填写保护伞犯罪内容',
            'captcha.require'   => '请填写验证码',
        ];
        
        $result = $this->validate($data,$rule,$msg);
        if(true !== $result){
            // 验证失败 输出错误信息
            $this->error($result);
        }
        $this->checkImagesAndVideos($data);
        $data['no'] = $this->createNo();

        $hshMod = (new Hsh);
        $fCharge = $hshMod->getFChargeList();
        $data['f_charge'] = array_intersect(array_keys($fCharge), $data['f_charge']);
        if (!$data['f_charge']) {
            $this->error('请选择涉嫌罪名');
        }
        $data['f_charge'] = implode(',', $data['f_charge']);
        $data['f_starttime'] = strtotime($data['f_starttime'].'0101');
        $data['f_endtime'] = strtotime($data['f_endtime'].'0101');
        $fIndustry = $hshMod->getFIndustryList();
        $data['f_industry'] = array_intersect(array_keys($fIndustry), $data['f_industry']);
        if (!$data['f_industry']) {
            $this->error('请选择涉及行业');
        }
        $data['f_industry'] = implode(',', $data['f_industry']);
        // isset($data['id'])
        // unset($data['id']);
        $id = (new Hsh)->gangCrimes($data);
        if ($id) {
            $this->success('成功', ['no'=>$data['no']]);
        }else{
            $this->error('失败');
        }
        
    }
    /**
     * 创建编号
     * @author 274589954@qq.com
     * @since  2022-07-02
     * @return [type]     [description]
     */
    protected function createNo()
    {
        return date('YmdHis').mt_rand(100000,999999);
    }
    /**
     * 养老保险诈骗
     * @author 274589954@qq.com
     * @since  2022-07-01
     * @return [type]     [description]
     */
    public function query()
    {
        $token = $this->request->header('token','');
        $type = $this->request->param('type','');
        $no = $this->request->param('no','');//编号
        $where =[
            'no'=>$no
        ];
        $data = null;
        if ($type == 'gangCrimes') {//扫黑
            $data = Hsh::where($where)
            // ->field('status')
            ->find();
        }elseif ($type == 'harmMinors') {//未成年
            $data = Wcn::where($where)
            // ->field('status')
            ->find();
        }elseif ($type == 'endowmentInsuranceFraud') {//老人
            $data = Yl::where($where)
            // ->field('status')
            ->find();
        }
        $param = $this->request->param();
        $rule = [
            'captcha'=>'require|captcha:api'.$token,
        ];
        $msg = [
            'captcha.require'   => '请填写验证码',
        ];
        $param = $this->request->param();
        $result = $this->validate($param,$rule,$msg);
        if(true !== $result){
            // 验证失败 输出错误信息
            $this->error($result);
        }
        $this->success('返回成功', $data);
    }
}
